package com.achun.ximalayamvvm.view.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.recyclerview.widget.RecyclerView
import com.achun.ximalayamvvm.BaseApplication
import com.achun.ximalayamvvm.R
import com.achun.ximalayamvvm.utils.LogUtil
import com.ximalaya.ting.android.opensdk.model.track.Track

class PopupListAdapter: RecyclerView.Adapter<PopupListAdapter.InnerHolder>() {
    class InnerHolder(itemView: View):
    RecyclerView.ViewHolder(itemView){
    var itemtitle=itemView.findViewById<TextView>(R.id.music_title)
    }
    private var playlist=ArrayList<Track>()
    private var playingIndex=0
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): PopupListAdapter.InnerHolder {
        var view =
            LayoutInflater.from(parent.context).inflate(R.layout.pop_list_item, parent, false)
        return InnerHolder(view)
    }

    override fun onBindViewHolder(holder: PopupListAdapter.InnerHolder, position: Int) {
        LogUtil.e(this,"刷新绑定--》$playingIndex")
        var trackTitle = playlist[position].trackTitle
        holder.itemtitle.text = trackTitle
        if (position==playingIndex){
            holder.itemtitle.setTextColor(BaseApplication.getAppContext().resources.getColor(R.color.nav_main))
        }else{
            holder.itemtitle.setTextColor(Color.Black.toArgb())
        }
        holder.itemView.setOnClickListener{
            onItemClickListenter.onClick(position)
        }
    }

    override fun getItemCount(): Int {
        return playlist.size
    }

    fun setDatas(tracks: List<Track>) {
        this.playlist= tracks as ArrayList<Track>
        notifyDataSetChanged()
        LogUtil.e(this,"列表刷新-----")
    }

    fun setPlayIndex(position: Int) {
        this.playingIndex=position
        LogUtil.e(this,"索引刷新----")
        notifyDataSetChanged()
    }
    interface OnItemClickListenter{
        fun onClick(position:Int)
    }
    private lateinit var onItemClickListenter: OnItemClickListenter
    fun setOnItemClickListenter(onItemClickListenter: OnItemClickListenter){
        this.onItemClickListenter=onItemClickListenter
    }
}